package com.ktjy.t382.ch01.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ktjy.t382.ch01.entity.vo.R;
import com.ktjy.t382.ch01.util.RandomStringGenerator;
import com.ktjy.t382.ch01.util.RedisUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
@Component
public class MyInterceptor implements HandlerInterceptor {
    @Resource
    ObjectMapper objectMapper;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String token=request.getHeader("token");
//        if(token==null||token.equals("")){
//            response.getWriter().println(objectMapper.writeValueAsString(R.fail("财富会低语，肚子会变大")));
//            return false;
//        }else {
//            RedisUtil redisUtil=new RedisUtil();
//            RandomStringGenerator generator=new RandomStringGenerator();
//            String s = generator.generateRandomString(6);
//            redisUtil.setKey("token",s);
//            String token1 = redisUtil.getKey("token");
//            System.out.println(token1);
//            return true;
//        }

        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
